package xyf.p04.tree;

import commons.TreeNode;

/**
 * ClassName: P0114_Convert2Node
 * Description:
 * Author: xyf
 * Date: 9/9/22 9:43 AM
 * Version: 1.0
 **/
public class P0114_Convert2Node
{
    private TreeNode dummyHead = new TreeNode(-1);

    private TreeNode tail = dummyHead;

    public void flatten(TreeNode root)
    {
        dfs(root);
    }

    private void dfs(TreeNode root)
    {
        if (root == null)
        {
            return;
        }

        TreeNode left = root.left;

        TreeNode right = root.right;

        tail.right = root;

        tail = root;

        tail.left = null;

        dfs(left);

        dfs(right);
    }
}